<template>
  <div style="width: 200px">
    <h1>例子</h1>
    <button @click="addItem">新增</button>

    <div class="list-container">
      <transition-group name="slide-fade" tag="ul">
        <li v-for="(item, index) in items" :key="item">
          {{ item }}
          <button @click="removeItem(index)">删除</button>
        </li>
      </transition-group>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AnimateDemo',
  data() {
    return {
      items: [],
    }
  },
  methods: {
    addItem() {
      const newItem = `项 ${this.items.length + 1}`
      this.items.push(newItem)
    },
    removeItem(index) {
      this.items.splice(index, 1)
    },
  },
}
</script>

<style lang="scss" scoped>
.list-container {
  margin: 20px 0;

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    border: 1px solid #ccc;
    margin-bottom: 5px;
    border-radius: 5px;
    transition: all 0.3s ease;
    animation: fadeIn 0.3s ease;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateX(100px);
    }
    to {
      opacity: 1;
      transform: translateX(0);
    }
  }

  .slide-fade-leave-active {
    animation: fadeOut 0.3s ease;
  }

  @keyframes fadeOut {
    from {
      opacity: 1;
      transform: translateX(0);
    }
    to {
      opacity: 0;
      transform: translateX(100px);
    }
  }
}
</style>
